Grokking Oracle Coding Interview
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
Who Should Take This Course?
Oracle Easy
Count Number of Pairs With Absolute Difference K (easy)
Solution: Count Number of Pairs With Absolute Difference K
Degree of an Array (easy)
Solution: Degree of an Array
Find Subsequence of Length K With the Largest Sum (easy)
Solution: Find Subsequence of Length K With the Largest Sum
Rectangle Overlap (easy)
Solution: Rectangle Overlap
Remove All Adjacent Duplicates In String (easy)
Solution: Remove All Adjacent Duplicates In String
Island Perimeter (easy)
Solution: Island Perimeter
Climbing Stairs (easy)
Solution: Climbing Stairs
Move Zeroes (easy)
Solution: Move Zeroes
Excel Sheet Column Title (easy)
Solution: Excel Sheet Column Title
Valid Parentheses (Easy)
Solution: Valid Parentheses
Longest Palindromic Substring (medium)
Solution: Longest Palindromic Substring
Single Element in a Sorted Array (medium)
Solution: Single Element in a Sorted Array
Pairs of Songs With Total Durations Divisible by 60 (medium)
Solution: Pairs of Songs With Total Durations Divisible by 60
Search a 2D Matrix II (medium)
Solution: Search a 2D Matrix II
Minimize Maximum of Array (medium)
Solution: Minimize Maximum of Array
Asteroid Collision (medium)
Solution: Asteroid Collision
Combination Sum (medium)
Solution: Combination Sum
Binary Tree Right Side View (medium)
Solution: Binary Tree Right Side View
Sort Colors (medium)
Solution: Sort Colors
Group Anagrams (Medium)
Solution: Group Anagrams
Decode String (Medium)
Solution: Decode String
Number of Islands (medium)
Solution: Number of Islands
Binary Tree Zigzag Level Order Traversal (medium)
Solution: Binary Tree Zigzag Level Order Traversal
Longest Common Subsequence (medium)
Solution: Longest Common Subsequence
Rotting Oranges (medium)
Solution: Rotting Oranges
Meeting Rooms II (medium)
Solution: Meeting Rooms II
Delete Node in a BST (medium)
Solution: Delete Node in a BST
Remove Duplicate Letters (medium)
Solution: Remove Duplicate Letters
Longest Substring Without Repeating Characters (medium)
Solution: Longest Substring Without Repeating Characters (medium)
Search in Rotated Array (medium)
Solution: Search in Rotated Array
Spiral Matrix (medium)
Solution: Spiral Matrix
Container With Most Water (Medium)
Solution: Container With Most Water
3Sum (medium)
Solution: 3Sum
Simplify Path (medium)
Solution: Simplify Path
Next Permutation (medium)
Solution: Next Permutation
Basic Calculator II (medium)
Solution: Basic Calculator II
Merge Intervals (medium)
Solution: Merge Intervals
Restore IP Addresses (medium)
Solution: Restore IP Addresses
Longest Increasing Subsequence (medium)
Solution: Longest Increasing Subsequence
Validate Binary Search Tree (medium)
Solution: Validate Binary Search Tree
Integer to English Words (hard)
Solution: Integer to English Words
Trapping Rain Water (hard)
Solution: Trapping Rain Water
First Missing Positive (hard)
Solution: First Missing Positive
Merge K Sorted Lists (hard)
Solution: Merge K Sorted Lists
Max Points on a Line (hard)
Solution: Max Points on a Line
Sudoku Solver (hard)
Solution: Sudoku Solver
Longest Valid Parentheses (hard)
Solution: Longest Valid Parentheses
Remove Nth Node From End of List (Medium)
Solution: Remove Nth Node From End of List
Median of Two Sorted Arrays (hard)
Solution: Median of Two Sorted Arrays
Constrained Subsequence Sum (hard)
Solution: Constrained Subsequence Sum
Solution: Rectangle Overlap

Problem Statement

You are given a rectangle as a list [x1, y1, x2, y2], where (x1, y1) is the coordinate of its bottom-left corner, and (x2, y2) is the coordinate of its top-right corner. The top and bottom edges of the rectangle are parallel to the X-axis, and the left and right edges are parallel to the Y-axis.

Two rectangles overlap if the rectangles share a part of the area; mere touching at edges or corners doesn't count.

Given two rectangles rec1 and rec2, return true if they overlap, otherwise return false.

Examples

  • Example 1:

    • Input: rec1 = [0, 0, 1, 1], rec2 = [1, 1, 2, 2]
    • Expected Output: false
    • Justification: The two rectangles touch at a corner but do not share any area, thus they do not overlap.
  • Example 2:

    • Input: rec1 = [0, 0, 2, 2], rec2 = [1, 1, 3, 3]
    • Expected Output: true
    • Justification: The rectangles share a part of their area, thus they overlap.
  • Example 3:

    • Input: rec1 = [1, 1, 3, 3], rec2 = [2, 2, 4, 4]
    • Expected Output: true
    • Justification: These rectangles share a central area, thereby confirming overlap.

Solution

To solve this problem, we approach it by checking the conditions under which rectangles do not overlap, which simplifies the problem significantly. We know that if one rectangle is to the left, right, above, or below the other, they do not overlap. This method is effective because it reduces the problem to simple comparisons of the x and y coordinates of the rectangles' corners.

Our approach involves checking if the maximum of the bottom-left x-coordinates is less than the minimum of the top-right x-coordinates and if the maximum of the bottom-left y-coordinates is less than the minimum of the top-right y-coordinates. If both conditions are true, the rectangles overlap. This method is both simple and efficient, avoiding unnecessary calculations and complexities involved in area computation or edge/corner cases.

Step-by-Step Algorithm

  1. Check Left: Verify if the right edge (x2 value) of the first rectangle (rec1) is to the left of the left edge (x1 value) of the second rectangle (rec2). This is done by comparing if rec1[2] <= rec2[0].

  2. Check Right: Determine if the left edge (x1 value) of rec1 is to the right of the right edge (x2 value) of rec2. This involves checking if rec1[0] >= rec2[2].

  3. Check Below: Check if the top edge (y2 value) of rec1 is below the bottom edge (y1 value) of rec2 by comparing if rec1[3] <= rec2[1].

  4. Check Above: Ascertain if the bottom edge (y1 value) of rec1 is above the top edge (y2 value) of rec2. This is done by verifying if rec1[1] >= rec2[3].

  5. Return Result: If none of the above conditions are true, it means that rec1 and rec2 overlap. Return true in this case. If any of the conditions are met, it implies that the rectangles do not overlap; hence, return false.

Algorithm Walkthrough with Example

Given rectangles rec1 = [1, 1, 3, 3] and rec2 = [2, 2, 4, 4]:

  1. Check Left: Compare if the right edge of rec1 (3) is to the left of the left edge of rec2 (2). This is not true since 3 is not less than or equal to 2.

  2. Check Right: Determine if the left edge of rec1 (1) is to the right of the right edge of rec2 (4). This condition is false because 1 is not greater than or equal to 4.

  3. Check Below: Check if the top edge of rec1 (3) is below the bottom edge of rec2 (2). This is false since 3 is not less than or equal to 2.

  4. Check Above: Ascertain if the bottom edge of rec1 (1) is above the top edge of rec2 (4). This condition is also false as 1 is not greater than or equal to 4.

  5. Return Result: Since none of the conditions for being to the left, right, above, or below are met, the rectangles must overlap. Therefore, for rec1 = [1, 1, 3, 3] and rec2 = [2, 2, 4, 4], the algorithm returns true, indicating that the rectangles overlap.

Code

Java

. . . .

Complexity Analysis

  • Time Complexity:

    • The time complexity of this algorithm is constant, , because the computation to determine if two rectangles overlap involves a fixed number of comparisons. No matter the input size or the specific values of the rectangle coordinates, the number of operations remains the same.
  • Space Complexity:

    • The space complexity is also constant, , as the algorithm operates directly on the input parameters without utilizing any additional data structures that scale with input size.
Rectangle Overlap (easy)
Remove All Adjacent Duplicates In String (easy)
Mark as Completed

On this page

Problem Statement

Examples

Solution

Step-by-Step Algorithm

Algorithm Walkthrough with Example

Code

Complexity Analysis